package com.UnionPayCenter.operation.controller;

import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.operation.entity.ActivityConfig;
import com.UnionPayCenter.operation.entity.dto.ActivityConfigDTO;
import com.UnionPayCenter.operation.entity.vo.ActivityConfigVO;
import com.UnionPayCenter.operation.enums.ActivityOperationTypeEnum;
import com.UnionPayCenter.operation.manager.ActivityConfigManager;
import com.UnionPayCenter.util.Asserts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 活动类型
 */
@Slf4j
@Api(value = "运营全局配置")
@CurrentManager
@RestController
@RequestMapping("/operation/activity/config")
public class ActivityConfigController {

    @Autowired
    private ActivityConfigManager activityTypeManager;

    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "[后台]新增运营活动配置", response = Integer.class)
    public ResponseWrapper addActivityConfig(@ApiParam @RequestBody(required = false) ActivityConfigDTO activityConfigDTO) {
        Asserts.notNull(activityConfigDTO, "活动配置");
        Asserts.naturalNumber(activityConfigDTO.getShopId(), "店铺Id");
        Asserts.notNull(activityConfigDTO.getActivityOperationType(), "活动操作类型");
        ActivityConfig activityConfig = new ActivityConfig();
        BeanUtils.copyProperties(activityConfigDTO, activityConfig);
        return ResponseWrapper.succeed(activityTypeManager.addActivityConfig(activityConfig));
    }

    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "[后台]修改运营活动配置", response = Integer.class)
    public ResponseWrapper updateActivityConfig(@ApiParam @RequestBody(required = false) ActivityConfigDTO activityConfigDTO) {
        Asserts.notNull(activityConfigDTO, "活动配置");
        Asserts.naturalNumber(activityConfigDTO.getShopId(), "店铺Id");
        //设置默认值
        activityConfigDTO.setActivityOperationType(Objects.isNull(activityConfigDTO.getActivityOperationType()) ?
                ActivityOperationTypeEnum.BALANCE_PAY : activityConfigDTO.getActivityOperationType());
        ActivityConfig activityConfig = new ActivityConfig();
        BeanUtils.copyProperties(activityConfigDTO, activityConfig);
        if(Objects.isNull(activityConfigDTO.getActivityConfigId())){
            activityTypeManager.addActivityConfig(activityConfig);
        }else {
            Asserts.naturalNumber(activityConfigDTO.getActivityConfigId(), "活动配置Id");
            activityTypeManager.updateActivityConfig(activityConfig);
        }
        return ResponseWrapper.succeed();
    }

    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "[后台]查询运营活动配置", response = ActivityConfigVO.class)
    public ResponseWrapper getActivityConfig(@ApiParam @RequestBody(required = false) ActivityConfigDTO activityConfigDTO) {
        Asserts.notNull(activityConfigDTO, "活动配置查询条件");
        return ResponseWrapper.succeed(activityTypeManager.getByShopId(activityConfigDTO.getShopId()));
    }

}
